from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QMessageBox, QWidget

from business.business import Business
from ui.role_name import Ui_Form


class RoleName(QWidget):
    SignalRoleName = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.business = Business()
        self.ui.pushButton.clicked.connect(self.confirm_new_role_name)

    def confirm_new_role_name(self):
        name = self.ui.lineEdit.text()
        if not name:
            msg_box = QMessageBox(QMessageBox.Question, '警告', '请填写角色名')
            msg_box.exec_()
        elif name in self.business.role_config_model:
            msg_box = QMessageBox(QMessageBox.Question, '警告', '角色名重复，请重新填写')
            msg_box.exec_()
        else:
            print('tui chu')
            self.SignalRoleName.emit(name)
            self.close()
